local sk__xujin = fk.CreateSkill {

  name = "sk__xujin",

  tags = {  },

}


sk__xujin:addEffect(fk.EventPhaseStart, {
  name = "sk__xujin",
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__xujin.name) and player.phase == Player.Draw
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(5)
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, sk__xujin.name, nil, true, player)
    local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1,
      "#sk__xujin-choose", sk__xujin.name, false)
    to = room:getPlayerById(to[1])
    local suits = {}
    for _, id in ipairs(cards) do
      table.insertIfNeed(suits, Fk:getCardById(id):getSuitString(true))
    end
    local _, choice = room:askToChooseCardsAndChoice(player, {  
      cards = cards,
      choices = suits,
      skill_name = sk__xujin.name,
      prompt = "#sk__xujin-get",
      cancel_choices = {},
      min = 0,
      max = 0,
    })
    local obtain, discard = {}, {}
    for _, id in ipairs(cards) do
      if Fk:getCardById(id):getSuitString(true) == choice then
        table.insert(obtain, id)
      else
        table.insert(discard, id)
      end
    end
    room:moveCardTo(obtain, Card.PlayerHand, to, fk.ReasonJustMove, sk__xujin.name, nil, true, to)
    if #discard > 0 then
      room:moveCardTo(discard, Card.DiscardPile, nil, fk.ReasonJustMove, sk__xujin.name, nil, true, player)
    end
    if not player.dead then
      room:setPlayerMark(player, "sk__xujin-turn", #obtain)
      room:addPlayerMark(player, MarkEnum.SlashResidue.."-turn", #obtain)
    end
    return true
  end,
})

sk__xujin:addEffect("atkrange", {
  name = "#sk__xujin_attackrange",
  correct_func = function (self, from, to)
    return from:getMark("sk__xujin-turn")
  end,
})

return sk__xujin